package controller;

import cn.edu.zut.dao.BookDao;
import cn.edu.zut.dto.BookAndType;
import cn.edu.zut.model.Book;
import cn.edu.zut.model.BookType;
import cn.edu.zut.model.BorrowBook;
import cn.edu.zut.service.BookService;
import cn.edu.zut.service.BookTypeService;
import cn.edu.zut.service.BorrowBookService;
import com.sun.org.apache.xpath.internal.operations.Mod;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpServletRequest;
import java.sql.Date;
import java.util.ArrayList;
import java.util.List;

@Controller
public class BookController{
    @Autowired
    BookService bookService;
    @Autowired
    BookTypeService bookTypeService;
    @Autowired
    BorrowBookService borrowBookService;

    //图书管理界面
    @RequestMapping(value = "bookManager")
    public String bookManager(HttpServletRequest request, Model model){
        List<BookAndType> books = bookService.selectBook();
        request.getSession().setAttribute("bookALL", books);
        return "admin/book/bookManager";
    }

    //增加图书
    @RequestMapping(value = "bookAddPre")
    public String bookAddPre(Model model){
        return "admin/book/bookadd";
    }

    @RequestMapping(value = "bookAdd" ,method = RequestMethod.GET)
    public String bookAdd(Book book, HttpServletRequest request, Model model){
        bookService.addBook(book);
        model.addAttribute("result","添加图书成功");
        return "forward:/bookPre";
    }

    //修改图书
    //获取id
    @RequestMapping(value = "bookUpdatePre/{id}")
    public String bookUpdatePre(@PathVariable("id") Integer id,Model model){
        Book book =  bookService.selectBookByBookid(id).get(0);
        List<BookType> bookTypes = bookTypeService.select_All_BookType();
        model.addAttribute("bookupdate",book);
        model.addAttribute("booktype",bookTypes);
        return "admin/book/bookmodify";
    }
    //修改图书信息
    @RequestMapping(value = "bookUpdate")
    public String bookUpdate(Book book,Model model){
        bookService.updateBook(book);
        model.addAttribute("result","修改图书成功");
        return "forward:/bookPre";
    }
    //修改图书状态
    @RequestMapping(value = "bookUpdateStatus")
    public String bookUpdateStatus(@RequestParam int bookid,@RequestParam int status,Model model){
        Book book = new Book();
        book.setBookid(bookid);
        book.setStatus(status);
        BorrowBook borrowBook = new BorrowBook();
        borrowBook.setBookId(bookid);
        borrowBookService.updateBorrowByBookId(borrowBook);
        bookService.updateBookStatus(book);
        model.addAttribute("result","修改图书状态成功");
        return "forward:/bookPre";
    }

    //修改图书状态
    @RequestMapping(value = "bookStatusPre/{id}")
    public String bookStatusPre(@PathVariable("id") Integer id,Model model){
        Book book =  bookService.selectBookByBookid(id).get(0);
        model.addAttribute("bookstatus",book);
        return "admin/book/bookstatus";
    }

    //删除图书
    @RequestMapping(value = "bookDelete/{id}")
    public String bookDelete(@PathVariable("id") Integer id, Model model){
        Book book = new Book();
        book.setBookid(id);
        bookService.deleteBook(book);
        model.addAttribute("result","删除图书成功");
        return "admin/book/bookManager";
    }

    //获取最新的数据库信息
    @RequestMapping(value = "bookPre")
    public String bookPre(HttpServletRequest request,Model model){
        List<BookAndType> books = bookService.selectBook();
        request.getSession().setAttribute("bookALL", books);
        return "admin/book/bookManager";
    }

    //查找前准备
    @RequestMapping(value = "bookSearch")
    public String bookSearch(@RequestParam String selectName,@RequestParam String search,
                             HttpServletRequest request,Model model){
        List<BookAndType> books = new ArrayList<>();
//        System.out.println(selectName);
//        System.out.println(search);
        switch (selectName){
            case "isbn":books = bookService.selectBookByISBN(search);break;
            case "书名":books = bookService.selectBookByName(search);break;
            case "作者":books = bookService.selectBookByAuthor(search);break;
            case "出版社":books = bookService.selectBookByPublish(search);break;
            case "类别": books = bookService.selectBookAndTypeByTypeid(search);break;
            default:return "admin/book/bookManager";
        }
        request.getSession().setAttribute("bookALL", books);
        return "admin/book/bookManager";
    }


}
